package cn.store.web.servlet;

import cn.store.domain.Category;
import cn.store.domain.PageModel;
import cn.store.domain.Product;
import cn.store.domain.User;
import cn.store.service.CategoryService;
import cn.store.service.ProductService;
import cn.store.service.UserService;
import cn.store.service.serviceImp.CategoryServiceImp;
import cn.store.service.serviceImp.ProductServiceImp;
import cn.store.service.serviceImp.UserServiceImp;
import cn.store.utils.UUIDUtils;
import cn.store.utils.UploadUtils;
import cn.store.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminServlet extends BaseServlet {
	//findAllProductsWithPage

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		UserService userService = new UserServiceImp();
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		String url = null;
		try {
			User userAdmin = userService.userLogin(user);
			if (userAdmin != null) {
				if (userAdmin.getState() == 2) {
					req.getSession().setAttribute("admin", userAdmin);
					resp.sendRedirect("admin/home.jsp");
					return null;
				} else {
					url = "/admin/index.jsp";
					req.setAttribute("message", "账户权限不足!");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			url = "/admin/index.jsp";
			req.setAttribute("message", "账号或密码错误!");
		}finally {
			return url;
		}
	}
}
